using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WPCoco;
using Microsoft.Xna.Framework.Input.Touch;
using WPCoco.Controllers;

namespace WPCoco.UI
{
    public class UIResponderController : IResponderHost
    {
        public UIResponderController(Node owner)
        {
            Owner = owner;
            Owner.IsRunningChange += new EventHandler(Owner_IsRunningChange);
        }

        #region Input

        void Owner_IsRunningChange(object sender, EventArgs e)
        {
            if (Owner.IsRunning)
                ConnectToTouchScreen();
            else
                DisconnectFromTouchScreen();
        }

        private void ConnectToTouchScreen()
        {
            ControllerManager.Shared.TouchScreen.StartDrag += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_StartDrag);
            ControllerManager.Shared.TouchScreen.Dragging += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Dragging);
            ControllerManager.Shared.TouchScreen.EndDrag += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_EndDrag);
            ControllerManager.Shared.TouchScreen.Tap += new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Tap);
        }

        private void DisconnectFromTouchScreen()
        {
            ControllerManager.Shared.TouchScreen.StartDrag -= new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_StartDrag);
            ControllerManager.Shared.TouchScreen.Dragging -= new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Dragging);
            ControllerManager.Shared.TouchScreen.EndDrag -= new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_EndDrag);
            ControllerManager.Shared.TouchScreen.Tap -= new EventHandler<CustomEventArgs<GestureSample>>(TouchScreen_Tap);
        }

        void TouchScreen_Tap(object sender, CustomEventArgs<GestureSample> e)
        {
            for (int i = 0; i < responders.Count; i++)
            {
                responders[i].PointerDown(e.Value.Position);
                responders[i].PointerUp(e.Value.Position);
            }
        }

        void TouchScreen_StartDrag(object sender, CustomEventArgs<GestureSample> e)
        {
            for (int i = 0; i < responders.Count; i++)
                responders[i].PointerDown(e.Value.Position);
        }

        void TouchScreen_Dragging(object sender, CustomEventArgs<GestureSample> e)
        {
            for (int i = 0; i < responders.Count; i++)
                responders[i].PointerMove(e.Value.Position);
        }

        void TouchScreen_EndDrag(object sender, CustomEventArgs<GestureSample> e)
        {
            for (int i = 0; i < responders.Count; i++)
                responders[i].PointerUp(e.Value.Position);
        }

        #endregion  

        public readonly Node Owner;

        readonly List<IResponder> responders = new List<IResponder>();

        #region IResponderHost

        public int Count
        {
            get { return responders.Count; }
        }

        public IResponder GetResponder(int index)
        {
            return responders[index];
        }

        public void AddResponder(IResponder responder)
        {
            responders.Add(responder);
        }

        public void RemoveResponder(IResponder responder)
        {
            responders.Remove(responder);
        }

        #endregion
    }
}
